package com.hcbd.tdwt.ui.xingyu.activity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.location.OnNmeaMessageListener;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.Listener.InfoInterface;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.ui.common.dialog.BottomSelectDialog;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.util.StringUtil;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGGAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGSVInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDRMCInfo;
import com.hcbd.tdwt.ui.xingyu.fragment.PositionInfoFragment_;
import com.hcbd.tdwt.ui.xingyu.fragment.SatelliteListFragment_;
import com.hcbd.tdwt.ui.xingyu.fragment.SkyMapFragment_;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
//星宇定位信息页面
@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_bd_position_info)
public class BdPositionActivity extends BaseActivity {
    private String TAG = "BdPositionActivity";
    @ViewById(R.id.position_info)
    RadioButton positionInfoRB;
    @ViewById(R.id.sky_map)
    RadioButton skyMapRB;
    @ViewById(R.id.satellite_list)
    RadioButton satelliteRB;
    @ViewById(R.id.position_mode)
    RelativeLayout positionRL;
    @ViewById
    TextView positionTv;
    PositionInfoFragment_ positionInfoFragment;
    SatelliteListFragment_ satelliteListFragment;
    SkyMapFragment_ skyMapFragment;
    SharedPreferencesUtil sp;
    private String mode;
    private List<SatelliteVO> satelliteVOList;
    private TreeMap<String, SatelliteVO> hashMap = new TreeMap<>();
    private  LocationManager mLocationManager;
    private final int LOCATION_CODE=15;
    private BottomSelectDialog bottomSelectDialog;
    private List<String>list=new ArrayList<>();
    @AfterViews
    protected void init() {
        satelliteVOList = new ArrayList<>();
        positionInfoFragment = new PositionInfoFragment_();
        satelliteListFragment = new SatelliteListFragment_();
        skyMapFragment = new SkyMapFragment_();
        positionInfoRB.setChecked(true);
        sp = new SharedPreferencesUtil(this);
        mode = sp.getAttribute(Constant.POSITION_MODE);
        initMsgRegister();
        if (Constant.PHONE_POS.equals(mode)) {
            positionTv.setText("手机定位");
        } else if (Constant.BD_POS.equals(mode)) {
            positionTv.setText("北斗设备定位");
            initMsgRegister();
        }
        list.add("手机定位");
        list.add("北斗设备定位");
        bottomSelectDialog=new BottomSelectDialog(this, "定位选择", list, (index, str) -> {
            if(index==0){
                positionTv.setText("手机定位");
                mode = Constant.PHONE_POS;
                hashMap.clear();
                sp.addAttribute(Constant.POSITION_MODE, Constant.PHONE_POS);
                EventBus.getDefault().unregister(this);
            }else{
                positionTv.setText("北斗设备定位");
                mode = Constant.BD_POS;
                hashMap.clear();
                sp.addAttribute(Constant.POSITION_MODE, Constant.BD_POS);
                initMsgRegister();
            }
        });

        this.setSkyInfoInterface(skyMapFragment);
        this.setSatelliteInfoInterface(satelliteListFragment);
        this.setPositionInterface(positionInfoFragment);
    }

    @Click(R.id.position_mode)
    void positionRL(){
        BottomSelectDialog.show(bottomSelectDialog);
    }

    @Click
    void headLeft(){
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDGSVInfo bdgsvInfo){
        initData(bdgsvInfo);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDGGAInfo bdggaInfo){
        positionInterface.onGGAInfo(bdggaInfo);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDRMCInfo bdrmcInfo){
        positionInterface.onRMCInfo(bdrmcInfo);
    }

    @CheckedChange(R.id.position_info)
    void positionInfoRB(boolean isChecked){
        if(isChecked) {
            switchFragment(positionInfoFragment, R.id.resultLL);
        }
    }
    @CheckedChange(R.id.sky_map)
    void skyMapRB(boolean isChecked){
        if(isChecked) {
            switchFragment(skyMapFragment, R.id.resultLL);
        }
    }
    @CheckedChange(R.id.satellite_list)
    void satelliteRB(boolean isChecked){
        if(isChecked) {
            switchFragment(satelliteListFragment, R.id.resultLL);
        }
    }

    private InfoInterface skyInfoInterface;
    private InfoInterface satelliteInfoInterface;
    private InfoInterface positionInterface;

    public void setSkyInfoInterface(InfoInterface infoInterface){
        this.skyInfoInterface=infoInterface;
    }
    public void setSatelliteInfoInterface(InfoInterface infoInterface){
        this.satelliteInfoInterface=infoInterface;
    }
    public void setPositionInterface(InfoInterface infoInterface){
        this.positionInterface=infoInterface;
    }
    public void initData(BDGSVInfo info){
        SatelliteVO satelliteVO=new SatelliteVO();
        if(StringUtil.isNumeric(info.sv1,info.azl1,info.elv1,info.cno1)){
            satelliteVO.setSv(info.sv1);
            satelliteVO.setCno(info.cno1);
            satelliteVO.setAz(info.azl1);
            satelliteVO.setElv(info.elv1);
            satelliteVO.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv1,satelliteVO);
        }
        if(StringUtil.isNumeric(info.sv2,info.azl2,info.elv2,info.cno2)) {
            SatelliteVO satelliteVO2 = new SatelliteVO();
            satelliteVO2.setSv(info.sv2);
            satelliteVO2.setCno(info.cno2);
            satelliteVO2.setAz(info.azl2);
            satelliteVO2.setElv(info.elv2);
            satelliteVO2.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv2, satelliteVO2);
        }
        if(StringUtil.isNumeric(info.sv3,info.azl3,info.elv3,info.cno3)) {
            SatelliteVO satelliteVO3 = new SatelliteVO();
            satelliteVO3.setSv(info.sv3);
            satelliteVO3.setCno(info.cno3);
            satelliteVO3.setAz(info.azl3);
            satelliteVO3.setElv(info.elv3);
            satelliteVO3.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv3, satelliteVO3);
        }
        if(StringUtil.isNumeric(info.sv4,info.azl4,info.elv4,info.cno4)) {
            SatelliteVO satelliteVO4 = new SatelliteVO();
            satelliteVO4.setSv(info.sv4);
            satelliteVO4.setCno(info.cno4);
            satelliteVO4.setAz(info.azl4);
            satelliteVO4.setElv(info.elv4);
            satelliteVO4.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv4, satelliteVO4);
        }
        satelliteVOList.clear();
        for (String key : hashMap.keySet()) {
            satelliteVOList.add(hashMap.get(key));
        }
        skyInfoInterface.onGSVInfo(satelliteVOList);
        satelliteInfoInterface.onGSVInfo(satelliteVOList);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case LOCATION_CODE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    // 权限被用户同意。
                    // 执形我们想要的操作
                } else {
                    toast("没有获取权限无法使用该功能！");
                }
            }
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}


